package dao

import (
	"errors"
	__ "user-srv/basic/proto"
	"user-srv/handler/models"
)

// 收藏添加
func AddCollect(in *__.AddCollectReq) (*models.Collect, error) {
	var user models.User
	user.Id = uint32(in.UserId)
	if err := user.GetUserInfoById(int(in.UserId)); err != nil {
		return nil, errors.New("查询用户失败")
	}
	if user.Id == 0 {
		return nil, errors.New("用户不存在")
	}
	var goods models.Goods
	goods.Id = uint32(in.GoodsId)
	if err := goods.GetGoodsById(int(in.GoodsId)); err != nil {
		return nil, errors.New("查询商品不存在")
	}
	if goods.Id == 0 {
		return nil, errors.New("商品不存在")
	}
	var collect models.Collect
	collect = models.Collect{
		UserId:  in.UserId,
		GoodsId: in.GoodsId,
	}
	if err := collect.AddCollect(); err != nil {
		return nil, errors.New("个人收藏添加失败")
	}
	return &collect, nil
}

// 收藏添加
func CollectInfo(in *__.CollectInfoReq) ([]*__.CollectInfo, error) {
	var collect models.Collect
	list, err := collect.GetCollect()
	if err != nil {
		return nil, errors.New("收藏展示失败")
	}
	var collectList []*__.CollectInfo
	for _, v := range list {
		collectList = append(collectList, &__.CollectInfo{
			Id:         v.Id,
			UserId:     v.UserId,
			GoodsId:    v.GoodsId,
			GoodsName:  v.GoodsName,
			GoodsPrice: float32(v.GoodsPrice),
			GoodsDesc:  v.GoodsDesc,
			GoodsUrl:   v.GoodsUrl,
		})
	}
	return collectList, nil
}

// 收藏删除
func DeleteCollect(in *__.DeleteCollectReq) (*models.Collect, error) {
	var collect models.Collect
	collect.Id = uint64(in.Id)
	if err := collect.GetCollectById(int(in.Id)); err != nil {
		return nil, errors.New("查询收藏不存在")
	}
	if collect.Id == 0 {
		return nil, errors.New("收藏不存在")
	}
	if err := collect.DeleteCollect(int(in.Id)); err != nil {
		return nil, errors.New("删除收藏失败")
	}
	return &collect, nil
}
